import sys

from PyQt5.QtWidgets import QFrame, QHBoxLayout, QLabel
from qfluentwidgets import LineEdit, FluentIcon
from components import OpenFileButton


class LineEditFrame(QFrame):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.hBox = QHBoxLayout(self)
        self.hBox.setContentsMargins(0, 0, 0, 0)
        self.hBox.setSpacing(10)

        self.bodyLabel = QLabel()
        self.bodyLabel.setStyleSheet('''font: 15px 'Segoe UI', 'Microsoft YaHei';''')
        self.bodyLabel.setText('选择文件:')
        self.bodyLabel.setFixedWidth(100)
        self.lineEdit = LineEdit()
        self.openFileButton = OpenFileButton()
        self.openFileButton.setIcon(FluentIcon.FOLDER)
        self.openFileButton.setFixedWidth(150)
        self.openFileButton.setText('选择文件夹')

        self.hBox.addWidget(self.bodyLabel)
        self.hBox.addWidget(self.lineEdit)
        self.hBox.addWidget(self.openFileButton)

        self.openFileButton.clicked.connect(self.openFileEvent)

    def setLabelText(self, text):
        self.bodyLabel.setText(text)
        self.lineEdit.setPlaceholderText(text[:-1] + '...')
        self.update()

    def setButtonText(self, text):
        self.openFileButton.setText(text)
        self.update()

    def setButtonIcon(self, icon):
        self.openFileButton.setIcon(icon)
        self.update()

    def openFileEvent(self):
        file = self.openFileButton.getExistingDirectory()
        self.lineEdit.setText(file)

    def lineEditText(self):
        return self.lineEdit.text()
